package fan.project3_pksystem.Module;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fan.project3_pksystem.Game.GameRequest;
import fan.project3_pksystem.Game.GameResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class GameRoomManager {
    ConcurrentHashMap<String, GameRoom> rooms = new ConcurrentHashMap<>();
    ConcurrentHashMap<Integer, String> players = new ConcurrentHashMap<>();

    


    public void add(GameRoom room,UserInfo user1 ,UserInfo user2) {
        rooms.put(room.getRoomId(), room);
        players.put(user1.getId(), room.getRoomId());
        players.put(user2.getId(), room.getRoomId());
    }

    public void remove(GameRoom room) {
        rooms.remove(room.getRoomId());
        players.remove(room.getUser1().getId());
        players.remove(room.getUser2().getId());
    }

    public GameRoom getRoomById(String roomId) {
        return rooms.get(roomId);
    }

    public GameRoom getRoomByUserId(Integer userId) {
        return getRoomById(players.get(userId));
    }
    public boolean getPlayerIsOnGame(Integer userId) {
        return players.get(userId) != null;
    }


}
